import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static junit.framework.TestCase.fail;

public class A {
	private WebDriver driver;
	private String baseUrl;
	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();

	@Before
	public void setUp() throws Exception {
		String value = "D:\\geckodriver-v0.16.1-win64\\geckodriver.exe";   //驱动浏览器jar包//在webapp/lib目录下
		String key="webdriver.gecko.driver";
		System.setProperty(key, value);
		driver = new FirefoxDriver();
		baseUrl = "https://www.teambition.com/";
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	}

	@org.junit.Test
	public void testA() throws Exception {
		driver.get(baseUrl + "/login");
		driver.findElement(By.name("email")).sendKeys("550187704@qq.com");
		driver.findElement(By.name("password")).sendKeys("123456789");
		driver.findElement(By.xpath("//button[@type='submit']")).click();
		Thread.sleep(3000);

		while (true) {
//		Thread.sleep(1000);
//		element.click();
			if (isElementPresent(By.xpath("//header/a"))) {
				driver.findElement(By.xpath("//header/a")).click();
			}

			Thread.sleep(2000);

			WebElement element = driver.findElement(By.xpath("//header[@id='navigator']/div/header/div/a/span"));
			element.click();
			driver.findElement(By.xpath("//body/div[2]/div/ul/li[2]")).click();

			if (isElementPresent(By.xpath("//header/a"))) {
				driver.findElement(By.xpath("//header/a")).click();
			}

			Thread.sleep(3000);

			driver.findElement(By.xpath("//div[@id='content']/div/div[2]/div[2]/div[2]/div/ul/li[5]/a/span[2]/div")).click();
			Thread.sleep(3000);
			switchToNewWindow();
			//div[@id='app']/div/div/div/section[2]/ul/li/span

			driver.findElement(By.xpath("//div[@id='app']/div/div/div/section[2]/ul/li/span")).click();
			Thread.sleep(1000);
			driver.findElement(By.linkText("删除企业")).click();
			Thread.sleep(1000);
			driver.findElement(By.linkText("确认删除")).click();
			Thread.sleep(1000);
			driver.findElement(By.cssSelector("div.actions > input.input")).clear();
			Thread.sleep(1000);
			driver.findElement(By.cssSelector("div.actions > input.input")).sendKeys("DELETE");
			Thread.sleep(1000);
			// ERROR: Caught exception [unknown command [send]]
			driver.findElement(By.linkText("确认删除")).click();
			Thread.sleep(3000);
		}
	}
	public void switchToNewWindow(){
		//得到当前句柄
		String currentWindow = driver.getWindowHandle();
		//得到所有窗口的句柄
		Set<String> handles = driver.getWindowHandles();

		//排除当前窗口的句柄，则剩下是新窗口
		Iterator<String> it = handles.iterator();
		while(it.hasNext()){
			if(currentWindow == it.next())  continue;
			driver.switchTo().window(it.next());
		}

	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	private boolean isAlertPresent() {
		try {
			driver.switchTo().alert();
			return true;
		} catch (NoAlertPresentException e) {
			return false;
		}
	}

	private String closeAlertAndGetItsText() {
		try {
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert) {
				alert.accept();
			} else {
				alert.dismiss();
			}
			return alertText;
		} finally {
			acceptNextAlert = true;
		}
	}
}
